﻿package com.msjt.action;

import java.util.List;

import javax.annotation.Resource;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.json.annotations.JSON;

import com.msjt.base.BaseAction;
import com.msjt.entity.News;
import com.msjt.service.INewsService;
import com.msjt.util.MyDate;
import com.msjt.util.PageMode;

/**
 * name:NewsAction function:新闻信息Action author: kw time:2013.8.26
 */
@SuppressWarnings("serial")
public class NewsAction extends BaseAction {
	@Resource
	private INewsService newsService;
	// model driver
	private News news;
	private PageMode page;

	// 置顶操作的结果信息
	private String message;

	@JSON(serialize = false)
	public PageMode getPage() {
		return page;
	}

	public void setPage(PageMode page) {
		this.page = page;
	}

	@JSON(serialize = false)
	public News getNews() {
		return news;
	}

	public void setNews(News news) {
		this.news = news;
	}

	@JSON(serialize = false)
	public INewsService getNewsService() {
		return newsService;
	}

	public void setNewsService(INewsService newsService) {
		this.newsService = newsService;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	// 分页
	// 前台新闻列表
	public String showMore() throws Exception {
		if (this.page == null) {
			this.page = new PageMode();
			this.page.setPageIndex(1);
		}
		this.page.setPageSize(10);
		this.page.setAction("news_showMore.action");
		this.page.setModelDrive("page");
		page.setSumRows(this.newsService.getNewsCount());
		page.createPageStr();
		List<News> newsList = this.newsService.getNewsMore(page.getPageIndex(),
				page.getPageSize());
		this.getRequest().put("newsList", newsList);
		this.getRequest().put("link", page.getPagingStr());
		this.page = null;
		return "moreNews";
	}

	// 后台新闻列表
	public String showAdmin() throws Exception {
		if (this.page == null) {
			this.page = new PageMode();
			this.page.setPageIndex(1);
		}
		this.page.setPageSize(10);
		this.page.setAction("news_showAdmin.action");
		this.page.setModelDrive("page");
		this.page.setSumRows(this.newsService.getNewsCount());
		this.page.createPageStr();
		this.getRequest().put(
				"newsList",
				this.newsService.getNewsAdmin(page.getPageIndex(),
						page.getPageSize()));
		this.getRequest().put("link", page.getPagingStr());
		this.page = null;
		return "adminNews";
	}

	// 批量删除
	public String deleteMore() throws Exception {
		String idString = ServletActionContext.getRequest().getParameter(
				"idString");
		idString = idString.substring(0, idString.length() - 1);
		boolean flag = this.newsService.deleteMore(idString);
		if (!flag) {
			this.getRequest().put("msg",
					"<script>alert('删除失败！');history.go(-1);</script>");
			return "error";
		}
		return showAdmin();
	}

	// 添加新闻
	public String addNews() throws Exception {
		this.news.setNewsDate(new MyDate().getDateString());
		boolean flag = this.newsService.addNews(this.news);
		if (!flag) {
			this.getRequest().put("msg",
					"<script>alert('添加失败！');history.go(-1);</script>");
			return "error";
		}
		return showAdmin();
	}

	// 新闻置顶
	// 异步刷新置顶,置顶后需要改变超链接的显示
	public String updateNewsType() throws Exception {
		boolean flag = this.newsService.updateTop(news);
		this.setMessage("失败");
		if (flag) {
			this.setMessage("成功");
		}
		return "input";
	}

	// 删除新闻
	public String deleteNews() throws Exception {
		boolean flag = this.newsService.deleteNews(this.news);
		if (!flag) {
			this.getRequest().put("msg",
					"<script>alert('删除失败！');history.go(-1);</script>");
			return "error";
		}
		// 删除新闻后刷新新闻列表
		return showAdmin();
	}

	// 后台得到详细新闻
	public String detailNews() throws Exception {
		News n = this.newsService.getNewsById(news);
		if (n == null) {
			this.getRequest().put("msg",
					"<script>alert('当前新闻不存在或已经被删除！');history.go(-1);</script>");
			return "error";
		}
		this.getRequest().put("n", n);
		return "detailNews";
	}

	// 准备修改
	public String preUpdate() throws Exception {
		News n = this.newsService.getNewsById(news);
		if (n == null) {
			this.getRequest().put("msg",
					"<script>alert('当前新闻不存在或已经被删除！');history.go(-1);</script>");
			return "error";
		}
		this.getRequest().put("n", n);
		return "updateNews";
	}

	// 修改新闻
	public String updateNews() throws Exception {
		boolean flag = this.newsService.updatedNews(this.news);
		if (!flag) {
			this.getRequest().put("msg",
					"<script>alert('修改失败！');history.go(-1);</script>");
			return "error";
		}
		return showAdmin();
	}

	// 根据ID查看新闻
	public String lookNews() throws Exception {
		News n = this.newsService.getNewsById(news);
		if (n == null) {
			this.getRequest().put("msg",
					"<script>alert('当前新闻不存在或已经被删除！');history.go(-1);</script>");
			return "error";
		}
		// 修改次数
		this.news.setNewsClick(n.getNewsClick());
		// 调用方法把次数持久化进数据库
		this.addClickNum();
		this.getRequest().put("n", n);
		return "newsDetail";
	}

	// 增加新闻次数
	public void addClickNum() throws Exception {
		this.newsService.updateClickNum(this.news);
	}

	// 模糊查询取得次数
	public int getNewsCountLike() {
		return this.newsService.getNewsCount(this.news);
	}

	// 模糊查询结果
	public String selectLike() throws Exception {
		if (this.page == null) {
			this.page = new PageMode();
			this.page.setPageIndex(1);
			this.page.setPageSize(10);
		}
		this.page.setAction("news_showAdmin.action");
		this.page.setModelDrive("page");
		this.page.setSumRows(this.newsService.getNewsCount(this.news));
		this.page.createPageStr();
		this.getRequest().put(
				"newsList",
				this.newsService.selectNewsLike(this.news, page.getPageIndex(),
						page.getPageSize()));
		this.getRequest().put("link", page.getPagingStr());
		return "adminNews";
	}
}
